layui.use('form', function () {

    var form = layui.form;

    layui.use('laydate', function () {
        var laydate = layui.laydate;
        laydate.render({
            elem: '#deadline' //指定元素
            , type: 'datetime'
        });
    });

    form.on('select(cell_type)', function (data) {
        switch (data.value) {
            case '1':
                $("#cell_content").html('<div class="layui-form-item">' +
                    '<label class="layui-form-label">优惠券号</label>' +
                    '<div class="layui-input-inline">' +
                    '<input class="layui-input" name="coupon_key" required lay-verify="required" autocomplete="off">' +
                    '</div>' +
                    '</div>' +
                    '            <div class="layui-form-item">' +
                    '                <label class="layui-form-label">面值</label>' +
                    '                <div class="layui-input-inline">' +
                    '                    <input class="layui-input" name="quota" required lay-verify="required" autocomplete="off">' +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="layui-form-item">' +
                    '                <label class="layui-form-label">发行总数</label>' +
                    '                <div class="layui-input-inline">' +
                    '                    <input class="layui-input" name="total" required lay-verify="required" autocomplete="off">' +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="layui-form-item">' +
                    '                <label class="layui-form-label">概率百分比</label>' +
                    '                <div class="layui-input-inline">' +
                    '                    <input class="layui-input" name="probability" required lay-verify="required" autocomplete="off">' +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="layui-form-item">' +
                    '                <label class="layui-form-label">有效期</label>' +
                    '                <div class="layui-input-inline">' +
                    '                    <select lay-filter="end_type" name="end_type">' +
                    '                        <option value="1">固定结束时间</option>' +
                    '                        <option value="2">固定持续时间</option>' +
                    '                    </select>' +
                    '                </div>' +
                    '                <div class="layui-input-inline">' +
                    '                    <div id="end_type_content">' +
                    '                        <input type="text" name="deadline" class="layui-input" lay-verify="required" id="deadline">\n' +
                    '                    </div>' +
                    '                </div>' +
                    '            </div>');

                form.render();
                layui.laydate.render({
                    elem: '#deadline' //指定元素
                    , type: 'datetime'
                });

                break;
            case '2':
                $("#cell_content").html('<div class="layui-form-item">' +
                    '                <label class="layui-form-label">发行总数</label>' +
                    '                <div class="layui-input-inline">' +
                    '                    <input class="layui-input" name="total" required lay-verify="required" autocomplete="off">' +
                    '                </div>' +
                    '            </div>' +
                    '<div class="layui-form-item">' +
                    '                <label class="layui-form-label">面值</label>' +
                    '                <div class="layui-input-inline">' +
                    '                    <input class="layui-input" name="quota" required lay-verify="required" autocomplete="off">' +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="layui-form-item">' +
                    '                <label class="layui-form-label">概率百分比</label>' +
                    '                <div class="layui-input-inline">' +
                    '                    <input class="layui-input" name="probability" required lay-verify="required" autocomplete="off">' +
                    '                </div>' +
                    '            </div>');

                break;
            case '3':

                $("#cell_content").html('<div class="layui-form-item">' +
                    '            <label class="layui-form-label">商品名</label>' +
                    '            <div class="layui-input-inline">' +
                    '                <input class="layui-input" name="goods_name" required lay-verify="required" autocomplete="off">' +
                    '            </div>' +
                    '        </div>' +
                    '        <div class="layui-form-item">' +
                    '            <label class="layui-form-label">发行总数</label>' +
                    '            <div class="layui-input-inline">' +
                    '                <input class="layui-input" name="total" required lay-verify="required" autocomplete="off">' +
                    '            </div>' +
                    '        </div>' +
                    '        <div class="layui-form-item">' +
                    '            <label class="layui-form-label">概率百分比</label>' +
                    '            <div class="layui-input-inline">' +
                    '                <input class="layui-input" name="probability" required lay-verify="required" autocomplete="off">' +
                    '            </div>' +
                    '        </div>');
                break;
            case '4':
                $("#cell_content").html('<div class="layui-form-item">\n' +
                    '                <label class="layui-form-label">概率百分比</label>\n' +
                    '                <div class="layui-input-inline">\n' +
                    '                    <input class="layui-input" name="probability" required lay-verify="required" autocomplete="off">\n' +
                    '                </div>\n' +
                    '            </div>');
                break;
            default:
                console.log("出错了")
                break;
        }
    })


    //监听下拉列表
    form.on('select(end_type)', function (data) {
        if (data.value == 1) {
            $('#end_type_content').html('<input type="text" name="deadline" class="layui-input" lay-verify="required" id="deadline" >');
            layui.use('laydate', function () {
                var laydate = layui.laydate;
                laydate.render({
                    elem: '#deadline' //指定元素
                    , type: 'datetime'
                });
            });
        } else {
            $('#end_type_content').html('<input type="text" name="duration" class="layui-input" lay-verify="required" autocomplete="off">');
        }
    });


    form.on('submit(cell_form)', function (data) {
        $.ajax({
            url: 'http://' + IPADDRESS + ':' + PORT + '/table/putActivityCell/',
            type: "POST",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify(data.field),
            dataType: "json",
            success: function (data) {
                if (data.header.res_code == '200000') {
                    layer.alert(data.header.message, {
                        icon: 1,
                        title: "提示"
                    });
                    setTimeout(function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                        }, 800
                    );

                } else {
                    layer.alert(data.header.message, {
                        icon: 5,
                        title: "提示"
                    });
                }
            }
        })
    })

});


